

<!-- treegrid部分 -->
<table id="dept_list"    title="部门管理" style="width:800px;"></table>

<!-- 条件查询、按钮 -->
<form  id="dept_search"  > 
	<div style="padding-top:2px;"></div>
	<a href="#" id="dept_add" onclick="javascript:;">添加</a>
    <a href="#" id="dept_edit" onclick="javascript:;">修改</a>
    <a href="#" id="dept_desel" onclick="javascript:;">取消选择</a>
    <a href="#" id="dept_del" onclick="javascript:;">删除</a>
</form>

<!-- 弹出窗口 detail -->
<div id="dept_popwin" class="easyui-dialog"
	style="width: 660px; height: 450px; padding: 10px 30px;"
	title="部门管理" buttons="#dept_popwin_buttons"
	data-options="modal:true,closed:true">
	 <h2><label>部门信息</label></h2>
     <form id="dept_detail_form" class="easyui-form" method="post">
     <input type="text" name="id"  id="id" style="width: 30px; display: none" />
		<table>
			<tr>
				<td>名称</td>
				<td><input  id="deptName" name="deptName"  
					style="width: 450px;"  class="easyui-textbox"
					data-options="required:true,validType:'length[1,60]'"  /></td></tr>
			<tr>
				<td>上级部门:</td>
				<td><input type="text" name="upDeptId"  id="upDeptId" style="width: 30px; display: none" /> 
				<input   id="upDeptName"  name="upDeptName"    class="easyui-textbox"   readonly="readonly"
					style="width: 450px;"  ></input></td>
			</tr>
			<tr>
				<td>所在单位:</td>
				<td><select id="emp_all_pbc" class="easyui-combotree"
				url="api/getallpbc"	  name="jgId" style="width: 450px;"  readonly="readonly"></select></td>
			</tr>
			

		</table>
     </form>
</div>
<div id="dept_popwin_buttons">
	<a href="#" id="dept_save" onclick="javascript:;">提交保存</a>
</div>

 
 
<script type="text/javascript">
var  dept_search= function(){
	var modelname="Dept";
	var pageName="dept";
   	var listPage=$("#"+pageName+"_list");
   	var search_form=pageName+"_search"
   	var submit_button=$("#"+pageName+"_search_submit");
   	var reset_button=$("#"+pageName+"_search_reset");
	var export_button=$("#"+pageName+"_export");
   	var popupWin=$("#"+pageName+"_popwin");
  //以下根据id获取按钮元素
	var query_button=$("#"+pageName+"_query");
	var add_button=$("#"+pageName+"_add");
	var edit_button=$("#"+pageName+"_edit");
	var del_button=$("#"+pageName+"_del");
	var save_button=$("#"+pageName+"_save");
   
	//编辑弹窗
	var detailForm=$("#"+pageName+"_detail_form");
   	var popupWin=$("#"+pageName+"_popwin");
   	listPage.treegrid({
	            url: 'api/user/getDeptTree',
	            singleSelect: true,
	            lines: true,
				rownumbers: true,
				idField: 'id',
				treeField: 'deptName',
	            fitColumns:true,
	            rownumbers:true,
	            toolbar:"#"+search_form,
	            pageSize: 15,
	            pageList: [5, 10, 15, 20, 30, 50, 80, 200],
	            columns:[[{
		            field: "id",
		            title: "编号",
		            hidden: true
		        },{
		            field: "deptName",
		            title: "名称",
		            align: 'center',
		            width: 80
		        }, 
		        {
		            field: "level",
		            title: "级别",
		            align: 'center',
		            width: 100,
		             
		        }]], 
	            
	    		onDblClickRow:function(index,row){
	    			loadDetail();
	    		} 
		        }); 
   
 
  reset_button.linkbutton({ iconCls: 'icon-clear', plain: true })
  .click(function () {
	  $(':input','#'+search_form)  
      .not(':button, :submit, :reset')  
      .val(' ')  
      .removeAttr('checked')  
      .removeAttr('selected');

  }); 
  $("#dept_desel").linkbutton({ iconCls: 'icon-clear', plain: true })
  .click(function () {
	  listPage.treegrid('clearSelections');
  });
  
//新增，初始化detail窗口后打开
  add_button.linkbutton({ iconCls: 'icon-add', plain: true })
  .click(function () {
  	addNewone();
  });
  
	//等同于双击treegrid中的row
  edit_button.linkbutton({ iconCls: 'icon-edit', plain: true })
  .click(function () {
  	loadDetail();
  });
  
	//删除一条记录
  del_button.linkbutton({ iconCls: 'icon-cut', plain: true })
  .click(function () {
  	var row = listPage.treegrid('getSelected');
		if (row){
			$.messager.confirm('系统提示','确认要删除么?',function(r){
			    if (r){
			        $.get('api/'+modelname+'/del',{id:row.id},function(data){
			        	var data = JSON.parse(data);
			        	$.messager.alert("系统提示",data.msg);
			        	listPage.treegrid("reload");					        	
			        });
			    }
			});		
		}else{
			$.messager.alert("系统提示","请先选中要删除的行！");
			return;
		}
  });
	//个性化编辑————————————保存数据，关闭detail窗口，刷新treegrid
  save_button.linkbutton({ iconCls: 'icon-ok', plain: true })
  .click(function () {
       save();
  });
  function addNewone(){
  	//去掉treegrid中的选择row
  	$("#deptName").textbox('setValue',"");
  	var row = listPage.treegrid('getSelected');
	 if (row){
			$("#upDeptId").val(row.id);
			$("#upDeptName").textbox('setValue',row.deptName);
			$("#emp_all_pbc").combotree('setValue',row.jgId);
		}
	 else
		 {	
		    $("#upDeptId").val("");
			$("#upDeptName").textbox('setValue',"");
			
			$('#emp_all_pbc').combotree('setValue',$.op.jgId);
			/* var url="api/user/getLoginUser";
			$.get(url, function(result){
				 result = JSON.parse(result);
				 //$('#emp_all_pbc').combotree({data:[{"id":result.jgId,"text":result.jgName,"state":"open"}]})
		    	 $('#emp_all_pbc').combotree('setValue',result.jgId);
		      }); */
		 }
	
	 //$.parser.parse(detailForm);
	 popupWin.dialog("open");
  }
  
  

	function save(){
		 var isValid =detailForm.form('validate');
	       if(!isValid){
	           return false;
	       }

		detailForm.form('submit',{
			url:'api/'+modelname+'/save',
			onSubmit:function(param){
				/* $.each(columnsAll,function(index,element){
			      if (element["type"]=="easyui-datebox")
			          { 
			            var temp= $("#"+pageName+"_"+element["field"]).datebox('getText').toString().split('-');	
			      		param[element["field"]]=moment({ years:temp[0], months:temp[1] - 1, date:temp[2]});		      		
			      		}
				}); */
				 
				 
			},
			success:function(data){
				var data = JSON.parse(data);
				console.log(data);
				//$.messager.alert("提示",data.msg);
				if (data.code == "0"){
					popupWin.dialog('close');
					listPage.treegrid("reload");
				}
			}
		});
	} 
 
 
  Array.prototype.remove = function(val) { 
	  var index = this.indexOf(val); 
	  if (index > -1) { 
	  this.splice(index, 1); 
	  } 
	  };
 
function loadDetail(){
	
	var row = listPage.treegrid('getSelected');
	if(!row)
		{
		$.messager.alert("系统提示","请先选中要修改的行。。。");
		return;
		}
	$.post('api/Dept/get',{id:row.id},function(result){
		detailForm.form("load",row);
		$("#id").val(row.id);
		if(result!==undefined&&result!=="")
		{
			result=JSON.parse(result)
			$("#upDeptId").val(result.id);
		    $("#upDeptName").textbox('setValue',result.deptName);
		}
		else
		{
			$("#upDeptId").val("");
		    $("#upDeptName").textbox('setValue',"");
		}
	}); 
	
		 
	 
		
		popupWin.dialog("open");
	 
}
 
/*  $(':input','#dept_detail_form')  
.not(':button, :submit, :reset')  
.val(' ')  
.attr('readonly','readonly'); */
  
}()
</script>